/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.gui.applicationframe.framesplitter.filetable;

import ftpcombiner.gui.applicationframe.framesplitter.FileTable;
import java.awt.Component;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.EventObject;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.table.TableCellEditor;

/**
 *
 * @author Ben
 */
public class FileTableEditor extends AbstractCellEditor implements TableCellEditor {

    JTextField editor;
    Object value;
    public static boolean renameableSelection = false;
    public static long time = 0;

    public FileTableEditor() {
        editor = new JTextField();
    }

    @Override
    public boolean isCellEditable(EventObject anEvent) {
        if (anEvent == null) {
            return true;
        }
        MouseEvent event = ((MouseEvent) anEvent);
        //System.out.println(event.getWhen() - time+": "+renameableSelection);
        if (event.getClickCount() < 2 && ((FileTable) event.getSource()).getSelectedRow() == (((FileTable) event.getSource()).rowAtPoint(new Point(event.getX(), event.getY()))) && !(((FileTable) event.getSource()).getSelectedRow() == 0) && event.getWhen() - time < 800L && renameableSelection) {
            return true;
        }
        renameableSelection = false;
        return false;
    }

    @Override
    public Object getCellEditorValue() {
        return null;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        this.value = value;
        editor.setText(value.toString());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                editor.selectAll();
            }
        });
        return editor;
    }
}
